package com.wkhyapp.lm.fragment;


import android.graphics.Color;
import android.graphics.Rect;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.lidong.photopicker.intent.PhotoPreviewIntent;
import com.lidong.photopicker.intent.PhotoPreviewIntent2;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.GoodsAdapter;
import com.wkhyapp.lm.base.SuperFragment;
import com.wkhyapp.lm.contract.DynamicPresenter;
import com.wkhyapp.lm.contract.DynamicView;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.view.GoodsInfoActivity;
import com.wkhyapp.lm.view.ShareGoodsActivity;
import com.wkhyapp.lm.view.ShopActivity;
import com.wkhyapp.lm.view.UpImageActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DTFragment extends SuperFragment<DynamicPresenter> implements DynamicView {


    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private GoodsAdapter goodsAdapert;
    private List<Goods> goods;
    private int mCurrentPage = 1;

    @Override
    public void initUI() {

        goods = new ArrayList<>();
        goodsAdapert = new GoodsAdapter(R.layout.item_goods_grid,goods);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == recycler_view.getAdapter().getItemCount() - 1) {
                    return 2;
                }
                return 1;
            }
        });

        recycler_view.setLayoutManager(manager);
        int space = getResources().getDimensionPixelSize(R.dimen.item_space);
        recycler_view.addItemDecoration(new SpaceItemDecoration(space));

        recycler_view.setAdapter(goodsAdapert);
        refresh_view.setRefreshing(false);
    }

    @Override
    protected void initListener() {
        goodsAdapert.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        goodsAdapert.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getDataMore(mCurrentPage);

            }
        });
        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getData();
            }
        });
        goodsAdapert.setCallBack(new GoodsAdapter.callBack() {
            @Override
            public void share(Goods item) {
                goTo(ShareGoodsActivity.class,item.getId());
            }

            @Override
            public void showImg(Goods item) {
                PhotoPreviewIntent2 intent = new PhotoPreviewIntent2(getActivity());
                intent.setCurrentItem(0);
                intent.setPhotoPaths(item.getGoodsImgs());
                startActivityForResult(intent, 1);
            }

            @Override
            public void info(Goods item) {
                goTo(GoodsInfoActivity.class,item.getId());
            }

            @Override
            public void shop(Goods item) {
                goTo(ShopActivity.class,item.getUserId());
            }

        });

    }

    @Override
    protected void initData() {
        mPresenter.getData();
    }


    @Override
    public void setGoods(List<Goods> goodsList) {
        goods.clear();
        if(goodsList!=null&&goodsList.size()>0){
            goods.addAll(goodsList);
        }
        goodsAdapert.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        goodsAdapert.loadMoreComplete();
    }


    @Override
    public void setGoodsMore(List<Goods> goodsMore) {
        if(goodsMore.size()==0){
            goodsAdapert.loadMoreComplete();
            goodsAdapert.loadMoreEnd();
        }else{
            goods.addAll(goodsMore);
            goodsAdapert.notifyDataSetChanged();
            goodsAdapert.loadMoreComplete();
        }
    }


    @Override
    protected DynamicPresenter createPresenter() {
        return mPresenter = new DynamicPresenter(this);
    }




    @Override
    public int getLayoutId() {
        return R.layout.fragment_dt;
    }


    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            //不是第一个的格子都设一个左边和底部的间距
            outRect.left = space;
            outRect.bottom = space;
            //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
            if (parent.getChildLayoutPosition(view) %3==0) {
                outRect.left = 0;
            }
        }

    }



}
